const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    selectAllFlag: false,
    totalPrice: 0,
    totalTaxFees:0,
    goodsArr: [
      {
        id: 1,
        title: '商品商品商品商品商品商品商品商品商品商品商品商品商品商品1',
        num: 1,
        price: 10,
        taxFees:1.2,
        selected: false,
        isTouchMove: false //默认隐藏删除
      },
      {
        id: 2,
        title: '商品2',
        num: 2,
        price: 5,
        taxFees: 1.0,
        selected: true,
        isTouchMove: false //默认隐藏删除
      },
      {
        id: 3,
        title: '商品3',
        num: 2,
        price: 15,
        taxFees: 2.2,
        selected: false,
        isTouchMove: false //默认隐藏删除
      },
    ],
    edit: false,
    startX: 0,
    startY: 0,
    dataArr:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    this.setData({
      dataArr:app.globalData.dataArr
    })
    this.countPrice()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (options) {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 减商品数量
  lowCart: function (e) {
    console.log("lowCart")
    const index = e.currentTarget.dataset.index;
    let goodsArr = this.data.goodsArr;
    let num = goodsArr[index].num;
    if (num <= 1) {
      return false
    }
    num = num - 1;
    goodsArr[index].num = num;
    this.setData({
      goodsArr: goodsArr
    })
    this.countPrice();
  },
  // 加商品数量
  addCart: function (e) {
    const index = e.currentTarget.dataset.index;
    let goodsArr = this.data.goodsArr;
    let num = goodsArr[index].num;
    num = num + 1;
    goodsArr[index].num = num;
    this.setData({
      goodsArr: goodsArr
    });
    this.countPrice()
  },
  // 单个选
  select: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var goodsArr = this.data.goodsArr;
    that.data.selectAllFlag = true;
    goodsArr[index].selected = !goodsArr[index].selected;
    for (var i = goodsArr.length - 1; i >= 0; i--) {
      if (!goodsArr[i].selected) {
        that.data.selectAllFlag = false;
        break
      }
    }
    that.setData({
      goodsArr: goodsArr,
      selectAllFlag: that.data.selectAllFlag
    });
    // 计算金额的方法
    that.countPrice();
  },
  // 全选
  selectAll: function (e) {
    var that = this;
    let selectAllFlag = this.data.selectAllFlag;
    selectAllFlag = !selectAllFlag;
    //获得购物车所有的商品
    let goodsArr = this.data.goodsArr;
    for (let i = 0; i < goodsArr.length; i++) {
      //设置每个商品的选中标识
      goodsArr[i].selected = selectAllFlag;
    }
    that.setData({
      selectAllFlag: selectAllFlag,
      goodsArr: goodsArr
    })
    // 计算金额的方法
    that.countPrice();
  },
  // 计算金额
  countPrice: function () {
    let goodsArr = this.data.goodsArr;
    let total = 0;
    let totalTaxFees = 0;
    for (let i = 0; i < goodsArr.length; i++) {
      if (goodsArr[i].selected) {
        total += goodsArr[i].num * goodsArr[i].price,
        totalTaxFees += goodsArr[i].num * goodsArr[i].taxFees
      }
      
    }
    this.setData({
      goodsArr: goodsArr,
      totalPrice: total.toFixed(2),
      totalTaxFees:totalTaxFees.toFixed(2)
    })
  },
  edit: function () {
    this.setData({
      edit: !this.data.edit,
    })
  },
  delete: function (e) {
    var that = this;
    const index = e.currentTarget.dataset.index;
    let goodsArr = this.data.goodsArr;
    wx.showModal({
      title: '提示',
      content: '确认删除吗',
      success: (res) => {
        if (res.confirm) {
          goodsArr.splice(index, 1)
          that.setData({
            goodsArr: goodsArr
          })
          if (!goodsArr.length) {
            that.setData({
              hasArr: false
            })
          } else {
            that.countPrice()
          }
        } else {
          console.log(res)
        }
      },
      fail: function (res) {
        console.log(res)
      }
    });
  },
  // 批量删除/选中删除
  deleteAll: function () {
    let goodsArr = this.data.goodsArr;
    let index = -1; //要开始删除的位置
    let delNum = 0; //要删除的个数
    for (let i = 0; i < goodsArr.length; i++) {
      if (goodsArr[i].selected) {
        if (index == -1) {
          index = i;
        }
        delNum++;
      }
    }
    goodsArr.splice(index, delNum);
    this.setData({
      goodsArr: goodsArr
    })
    this.countPrice()
  },

  //开始滑动
  touchstart: function (e) {
    console.log(e)
    console.log("touchstart")
    this.data.goodsArr.forEach(function (v, i) {
      if (v.isTouchMove)
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      goodsArr: this.data.goodsArr
    })
    return false
  },
  //滑动中
  touchmove: function (e) {
    console.log(e)
    var that = this;
    var index = e.currentTarget.dataset.index,
      startX = that.data.startX,
      startY = that.data.startY,
      touchMoveX = e.changedTouches[0].clientX,
      touchMoveY = e.changedTouches[0].clientY,
      angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
    that.data.goodsArr.forEach(function (v, i) {
      v.isTouchMove = false
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX)
          v.isTouchMove = false
        else
          v.isTouchMove = true
      }
    })
    that.setData({
      goodsArr: that.data.goodsArr
    })
    return false
  },
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI)
  },
  //移入收藏功能
  collect: function (e) {

  },
  toAccount: function () {
    wx.navigateTo({
      url: '../order-confirm/order-confirm' // 指定页面的url
    });
  }
})